//
//  ADViewController.m
//  ADKit
//
//  Created by 杜 泽旭 on 2017/12/2.
//  Copyright © 2017年 duzexu. All rights reserved.
//

#import "ADViewController.h"
#import "ADKitHeaders.h"

@interface ADViewController ()
// viewModel
@property (nonatomic, readwrite, strong) ADViewModel *viewModel;

@end

@implementation ADViewController

// when `BaseViewController ` created and call `viewDidLoad` method , execute `bindViewModel` method
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    ADViewController *viewController = [super allocWithZone:zone];
    @weakify(viewController)
    [[viewController
      rac_signalForSelector:@selector(viewDidLoad)]
     subscribeNext:^(id x) {
         @strongify(viewController)
         [viewController bindViewModel];
     }];
    return viewController;
}

+ (instancetype)controllerWithViewModel:(ADViewModel *)viewModel {
    UIStoryboard *storyboard = [viewModel storyboard];
    if (storyboard) {
        ADViewController *vc = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([self class])];
        vc.hidesBottomBarWhenPushed = viewModel.prefersTabBarHidden;
        vc.viewModel = viewModel;
        viewModel.viewController = vc;
        return vc;
    }else{
        ADViewController *vc = [[self alloc] initWithViewModel:viewModel];
        vc.hidesBottomBarWhenPushed = viewModel.prefersTabBarHidden;
        return vc;
    }
}

- (instancetype)initWithViewModel:(ADViewModel *)viewModel {
    self = [super init];
    if (self) {
        self.viewModel = viewModel;
        self.viewModel.viewController = self;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    IQKeyboardManager.sharedManager.enable = self.viewModel.keyboardEnable;
    IQKeyboardManager.sharedManager.shouldResignOnTouchOutside = self.viewModel.shouldResignOnTouchOutside;
    IQKeyboardManager.sharedManager.keyboardDistanceFromTextField = self.viewModel.keyboardDistanceFromTextField;
    [self.navigationController setNavigationBarHidden:self.viewModel.prefersNavigationBarHidden animated:animated];
    [self.viewModel viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.viewModel viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = !self.viewModel.interactivePopDisabled;
    }
    [self.viewModel viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.viewModel viewDidDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    /// ignore adjust auto scroll 64
    if (@available(iOS 11.0, *)) {
        self.automaticallyAdjustsScrollViewInsets = YES;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    UIImage *leftNavigationBarImage;
    if (self.viewModel.leftNavigationBarImage) {
        leftNavigationBarImage = self.viewModel.leftNavigationBarImage;
    }
    if (!leftNavigationBarImage) {
        leftNavigationBarImage = UINavigationBar.leftNavigationBarImage;
    }
    if (leftNavigationBarImage && !self.viewModel.prefersNavigationBackBtnHidden) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftNavigationBarImage style:UIBarButtonItemStylePlain target:self.viewModel action:@selector(leftNavigationBarButtonItemAction:)];
    }
    UIImage *rightNavigationBarImage;
    if (self.viewModel.rightNavigationBarImage) {
        rightNavigationBarImage = self.viewModel.rightNavigationBarImage;
    }
    if (!rightNavigationBarImage) {
        rightNavigationBarImage = UINavigationBar.rightNavigationBarImage;
    }
    if (rightNavigationBarImage) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightNavigationBarImage style:UIBarButtonItemStylePlain target:self.viewModel action:@selector(rightNavigationBarButtonItemAction:)];
    }
    if (self.viewModel.rightNavigationBarTitle) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:self.viewModel.rightNavigationBarTitle style:UIBarButtonItemStylePlain target:self.viewModel action:@selector(rightNavigationBarButtonItemAction:)];
    }
    [self.navigationItem setHidesBackButton:self.viewModel.prefersNavigationBackBtnHidden];
    [self initialize];
    [self.viewModel viewDidLoad];
}

- (void)initialize {}

// bind the viewModel
- (void)bindViewModel {
    RAC(self.navigationItem, title) = RACObserve(self, viewModel.title);
    RAC(self, title) = RACObserve(self, viewModel.title);
    [self.viewModel.errors subscribeNext:^(NSError *error) {
        NSLog(@"发生错误%@",error);
    }];
}

- (void)dealloc {
    ADLog(@"viewController [%@] dealloc",self.class);
}

#pragma mark - Orientation
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return UIInterfaceOrientationMaskPortrait;}
- (BOOL)shouldAutorotate {return YES;}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return UIInterfaceOrientationPortrait;}

#pragma mark - Status bar
- (BOOL)prefersStatusBarHidden {
    if (self.presentedViewController) {
        return self.presentedViewController.prefersStatusBarHidden;
    }
    return self.viewModel.prefersStatusBarHidden;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.presentedViewController) {
        return self.presentedViewController.preferredStatusBarStyle;
    }
    return self.viewModel.preferredStatusBarStyle;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { return UIStatusBarAnimationFade; }

@end
